/**
 * Created by zzw on 2016/5/18.
 * 设置新密码
 */

$(function(){
    //禁止密码复制粘贴
    $("#newPwd1").bind("cut copy paste", function(e){
        e.preventDefault();
    });
    $("#newPwd2").bind("cut copy paste", function(e){
        e.preventDefault();
    });
    setNewPwd();
});



//设置新密码方法
function setNewPwd(){
    function getUrlPage(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }
    var tel_mail = getUrlPage('tel_mail');
    if(tel_mail .indexOf("@")<0){
        $("#submit").click(function(){
            var reg1 = /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/;
            var reg2 = /^[^ ]$/;
            var newPwd1 = $("#newPwd1").val();
            var newPwd2 = $("#newPwd2").val();
            if(newPwd1==""||newPwd1==null){
                alert("密码不能为空！");
                return false;
            }
            if(/[`,~,!,@,#,$,%,^,&,*,\(,),\[,\],{,},\|,:,;,',",<,>,+,-,！,@,#,￥,%,……,*,（,）,\【,\】,｛,｝,\|,：,；,‘,’,“,”,《,》,，,。,\s]/.test(newPwd1)){
                alert("新密码不能包含特殊字符");
                return false;
            }
            if (!reg1.test(newPwd1)) {
                alert("新密码必须以6-20个字母、数字、下划线或减号，以字母开头组合");
                $("#newPwd1").val("");
                return false;
            }
            if(reg2.test(newPwd1)){
                alert("密码不能包含空格！");
                return false;
            }
            if(newPwd1!=newPwd2){
                alert("两次密码不一，请重新输入！");
                return false;
            }
            var param = JSON.stringify({"newPwd": newPwd1,"mail": tel_mail});
            $.fn.serviceInvoke(serviceAddress.Address, "agent_app_getBackPwd", 'updateMobilePwd.action', param, function (result) {
                if(result.msg=="success"){
                    alert("密码设置成功！");
                    window.location.href="../index.html";
                }else{

                } alert("密码设置失败！");
            })
        });
    }else{
        $("#submit").click(function(){
            var newPwd1 = $("#newPwd1").val();
            var newPwd2 = $("#newPwd2").val();
            if(newPwd1==""||newPwd1==null){
                alert("密码不能为空！");
                return false;
            }else {
                return true;
            }
            if(newPwd1!=newPwd2){
                alert("两次密码不一，请重新输入！");
                return false;
            }else {
                return true;
            }
            var param = JSON.stringify({"newPwd": newPwd1,"mail": tel_mail});
            $.fn.serviceInvoke(serviceAddress.Address, "agent_app_getBackPwd", 'updatePwd.action', param, function (result) {
                if(result.msg=="success"){
                    alert("密码设置成功！");
                    window.location.href="../index.html";
                }else{
                    alert("密码设置失败！");
                }
            })
        });
    }


}
